//## 제품 정보 저장 ####################
function fnSave()
{
	if (!$("#prdNm").val())
	{
		alert("제품명을 바르게 입력해 주세요!");
		$("#prdNm").focus();
		return;
	}
	var valUrl  = '/action/product/save.asp';
	var param  = "prdNm="+encodeURIComponent($("#prdNm").val());
		param += "&etcPrdCd="+encodeURIComponent($("#etcPrdCd").val());
		param += "&remarks="+encodeURIComponent($("#remarks").val());
		param += "&sourceSize="+encodeURIComponent($("#sourceSize").val());
		param += "&compCd="+$("#compCd").val();
		param += "&flag="+$("#flag").val();
		param += "&prdCd="+$("#prdCd").val();
	
	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			fnSessionCheck($(xml).find("MSG").text());
			if ($(xml).find("MSG").text() == "Save OK!")
			{
				alert("Save OK!");
				fnReset();
				grid01List();
			}
			else alert($(xml).find("MSG").text());
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 제품 정보 그리드 표시 ####################

var G_grid01Rslt = "" //XML Data를 받을 전역변수 선언

function grid01List()
{
	//setNullRow("loadSpan","1");
	var valUrl = "/action/product/list.asp";
	var param = "findPrdNm="+encodeURIComponent($("#findPrdNm").val());
		param += "&findEtcPrdCd="+encodeURIComponent($("#findEtcPrdCd").val());
		param += "&findMkType="+$("#findMkType").val();
		param += "&findGoods="+$("#findGoods").val();

	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());

			G_grid01Rslt = xml;
			setGrid01(1);
			fnReset();
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

function setGrid01(curPage)
{
    var cellWidth = new Array();
    var cellClass = new Array();
	for (var c=0; c<$("#Grid01 td").length; c++)
	{
		cellWidth[c] = $("#Grid01 td").eq(c).attr("width");
		cellClass[c] = $("#Grid01 td").eq(c).attr("class").replace(" gridTitle","");
	}

	//$("#tr01Result").css("display","none");
	var nodeCnt = $(G_grid01Rslt).find("Products").find("PrdList").length;
	if(nodeCnt == 0)
	{
		var rsltMsg = "<tr bgcolor=\"#FFFFFF\" height=\"30\" align=\"center\">";
		rsltMsg += "		<td colspan=\"7\" style=\"color:#948D88; BORDER-TOP: #abccf8 0px solid;height:35px\" class=\"lTD_0rbl\">";
		rsltMsg += "		검색된 자료가 없습니다.";
		rsltMsg += "		</td>";
		rsltMsg += "</tr>";
		$("#tr01Result").html(rsltMsg);
		$("#grid01Cnt").html("0");
		$("#pageDiv01").html("");
		return;
	}
	var listNum = 10;
	var totalCount = nodeCnt;
	$("#grid01Cnt").html(totalCount);
	if (curPage == "") curPage = 1;
	var totalPage = Math.ceil(nodeCnt/listNum);
	var restArticle = (nodeCnt%listNum);
	startNo = (parseInt(curPage)-1) * listNum;	
	var endNo = startNo + listNum;
	
	nodeCnt = nodeCnt - ((curPage-1)*listNum);
	if (totalPage == curPage)
	{
		if (restArticle == 0) var endNo = startNo + listNum;
		else var endNo = startNo + restArticle;
	}

	var pages = fnPaging("Grid01", curPage, totalPage);

	$("#pageDiv01").html(pages);
	
	var grid01Array = new Array();
	var chkCnt = 0;
	
	$("#tr01Result").html("");
	for(var j=startNo; j<endNo; j++)
	{
		var prdCd			= $(G_grid01Rslt).find("Products").find("PrdList").eq(j).find("prdCd").text();
		var prdNm			= $(G_grid01Rslt).find("Products").find("PrdList").eq(j).find("prdNm").text();
		var etcPrdCd		= $(G_grid01Rslt).find("Products").find("PrdList").eq(j).find("etcPrdCd").text();
		var company			= $(G_grid01Rslt).find("Products").find("PrdList").eq(j).find("company").text();
		var part			= $(G_grid01Rslt).find("Products").find("PrdList").eq(j).find("part").text();
		var remarks			= $(G_grid01Rslt).find("Products").find("PrdList").eq(j).find("remarks").text();		
		var flag			= $(G_grid01Rslt).find("Products").find("PrdList").eq(j).find("flag").text();
		var totalGoods			= $(G_grid01Rslt).find("Products").find("PrdList").eq(j).find("totalGoods").text();

		ji = j+1;
		
		var showFlag = "";
		var tagFirst = "";
		var tagLast = "";
		if(flag == "Y") showFlag = "생산";
		else if(flag == "H") showFlag = "중지";
		else showFlag = "삭제";

		var showGoods = totalGoods;
		if (totalGoods.split(",").join("")<1000 && totalGoods.split(",").join("")>0) showGoods = "<font color=\"#009900\">"+totalGoods+"</goods>";
		if (totalGoods.split(",").join("")<0) showGoods = "<font color=\"#ff0000\">"+totalGoods+"</goods>";
		
		if (flag == "N") { var bgColor = "#F2F2F2"; tagFirst = "<font color=\"#CCCCCC\">"; tagLast = "</font>"; }
		else var bgColor = "#FFFFFF";
		
		var curRows = "<tr style=\"cursor:pointer;background-color:"+bgColor+"\" onClick=\"fnSetProduct('"+prdCd+"')\">";
		curRows += "<td style=\"height:35px;text-align:center;\" width=\""+cellWidth[0]+"\" class=\""+cellClass[0]+"\">"+ji+"</td>";
		curRows += "<td style=\"height:35px;\" width=\""+cellWidth[1]+"\" class=\""+cellClass[1]+"\">"+tagFirst + prdNm + tagLast+"</td>";
		curRows += "<td style=\"height:35px;text-align:center;\" width=\""+cellWidth[2]+"\" class=\""+cellClass[2]+"\">"+tagFirst + etcPrdCd+"&nbsp;" + tagLast+"</td>";
		curRows += "<td style=\"height:35px;text-align:center;\" width=\""+cellWidth[3]+"\" class=\""+cellClass[3]+"\">"+tagFirst + company+"&nbsp;" + tagLast+"</td>";
		curRows += "<td style=\"height:35px;text-align:center;\" width=\""+cellWidth[4]+"\" class=\""+cellClass[4]+"\">"+tagFirst + showGoods + tagLast+"</td>";
		curRows += "<td style=\"height:35px;text-align:center;\" width=\""+cellWidth[5]+"\" class=\""+cellClass[5]+"\">"+tagFirst + remarks+"&nbsp;" + tagLast+"</td>";
		curRows += "<td style=\"height:35px;text-align:center;\" width=\""+cellWidth[6]+"\" class=\""+cellClass[6]+"\">"+showFlag+"</td>";
		curRows += "</tr>";


		$("#tr01Result").append(curRows);
	}
}


//## 거래처 상세정보 표시 ####################
function fnSetProduct(prdCd)
{
	var valUrl = "/action/product/view.asp";
	var param = "prdCd="+prdCd;

	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());
			
			$("#prdCd").val($(xml).find("prdCd").text());
			$("#prdNm").val($(xml).find("prdNm").text());
			$("#etcPrdCd").val($(xml).find("etcPrdCd").text());
			//$("#part").val($(xml).find("part").text());
			$("#remarks").val($(xml).find("remarks").text());
			$("#sourceSize").val($(xml).find("sourceSize").text());
			$("#flag").val($(xml).find("flag").text());

			fnFindCompany('',$(xml).find("compCd").text(),'compCd');			

		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 제품 정보 Reset 기능
function fnReset()
{

	$("#prdCd").val("");
	$("#prdNm").val("");
	$("#etcPrdCd").val("");
	$("#compNm").val("");
	$("#sourceSize").val("");
	$("#compCd").val("");
	//$("#part").val("");
	$("#remarks").val("");
	$("#flag").val("Y");
	$("#accessInfo").val("");
}


//## 검색 Reset 기능
function fnSearchReset()
{
	$("#findPrdNm").val("");
	$("#findEtcPrdCd").val("");
	$("#findType").val("");
	$("#findGoods").val("0");
}